Problem Statement:

You are tasked with designing a system that adheres to the Interface Segregation Principle (ISP) from the SOLID principles. The Interface Segregation Principle states that a class should not be forced to implement interfaces it does not use. The goal is to create a system with small, specific interfaces that are implemented only by classes that need them, preventing unnecessary dependencies.

Solution:

  1. Define Small, Specific Interfaces:

    public interface Worker {
        void work();
        void eat();
    }
    public interface Workable {
        void work();
    }
    public interface Feedable {
        void eat();
    }
  2. Create Classes Implementing Specific Interfaces:

    public class WorkerImpl implements Worker {
        @Override
        public void work() {
            System.out.println("Worker is working.");
        }
    
        @Override
        public void eat() {
            System.out.println("Worker is eating.");
        }
    }
    public class WorkOnlyWorker implements Workable {
        @Override
        public void work() {
            System.out.println("Work-only worker is working.");
        }
    }
    public class EatOnlyWorker implements Feedable {
        @Override
        public void eat() {
            System.out.println("Eat-only worker is eating.");
        }
    }
  3. Example Usage:

    public class Main {
        public static void main(String[] args) {
            Worker worker = new WorkerImpl();
            worker.work();
            worker.eat();
    
            Workable workOnlyWorker = new WorkOnlyWorker();
            workOnlyWorker.work();
    
            Feedable eatOnlyWorker = new EatOnlyWorker();
            eatOnlyWorker.eat();
        }
    }